import sys

from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QApplication, QWidget, QMessageBox, QFileDialog
from PyQt5 import QtGui
from ui import Ui_Form
from huffman import *


class My_UI(QWidget, Ui_Form):
    def __init__(self):
        super(My_UI, self).__init__()
        self.setupUi(self)
        self.label.setStyleSheet("color:rgb(10,10,10,255);font-size:25px;font-weight:bold;font-family:Roman times;")
        self.load_button.clicked.connect(self.load_file)
        self.ecoding_button.clicked.connect(self.Encoding)
        self.decoding_button.clicked.connect(self.Decoding)

    def load_file(self):
        self.file, _ = QFileDialog.getOpenFileName(self, 'open file', './', 'All Files (*);;Text Files (*.txt)')
        self.lineEdit.setText(self.file)
        print(self.file)

    def Encoding(self):
        file = self.lineEdit.text()
        encode_file(file)
        QMessageBox.information(self, 'information', '压缩成功！')

    def Decoding(self):
        file = self.lineEdit.text()
        if file.split('.')[-1] != 'ys':
            QMessageBox.critical(self, 'wrong', '请选择正确的压缩文件格式')
            return

        decode_file(file)
        QMessageBox.information(self, 'information', '解压成功！')





if __name__=='__main__':
    app = QApplication(sys.argv)
    my_ui = My_UI()
    my_ui.show()
    sys.exit(app.exec_())
